function initPage() {
    initInputText();
    initStripeTable();
}

function initStripeTable() {
    $(".stripe tr").mouseover(function () {
        $(this).addClass("over");
    })
        .mouseout(function () {
            $(this).removeClass("over");
        });
    $(".stripe tr:even").addClass("alt");
}

function initTabsCSS() {
    $("ul.tabs li:first").addClass("active");
    $("div.pages div.page:not(:first)").hide();
    $("ul.tabs li").click(function () {
        $(this).addClass("active").siblings("li").removeClass("active");
        $("#" + $(this).attr("page")).show().siblings("div.page").hide();
    });
}

function isValid() {
    var result = true;
    var requireText = "";
    $("input[required], textarea[required], select[required]").each(function () {
        if (result && ((jQuery.trim($(this).val()) == "") || (jQuery.trim($(this).val()) == jQuery.trim($(this).attr("placeHolder"))))) {
            alert($(this).attr("requireText"));
            $(this).focus();
            result = false;
        }
    });

    if (result && ($(".selectable").length > 0) && ($(".selectable[checked]").length == 0)) {
        alert($(this).attr("requireText"));
        result = false;
    }
    return result;
}

function getRootWin() {
    var win = window;
    while (win != win.parent) {
        win = win.parent;
    }
    return win;
}

function GetDateStr(AddDayCount) {
    var dd = new Date();
    dd.setDate(dd.getDate() + AddDayCount);//获取AddDayCount天后的日期
    var y = dd.getFullYear();
    var m = dd.getMonth() + 1;//获取当前月份的日期
    var d = dd.getDate();
    return y + "-" + m + "-" + d;
}

//function loadPage(id, url, language="english") {
function loadPage(parameters) {
    var id = parameters.id;
    var type = parameters.type;
    var language = parameters.language;
    var url = parameters.url;
    var async = parameters.async;
    var cache = parameters.cache;
    var data = parameters.data;
    var dataType = parameters.dataType;

    if ("undefined" == typeof type) {
        type = "POST";
    }
    if ("undefined" == typeof async) {
        async = true;
    }
    if ("undefined" == typeof language) {
        language = "chinese";
    }
    if ("undefined" == typeof cache) {
        cache = false;
    }
    if ("undefined" == typeof dataType) {
        dataType = 'html';
    }
    //alert(type);
    //alert(id);
    //alert(url);
    //alert(cache);
    $("#" + id).empty();
    //$("#" + id).addClass("loader");
    //$("#"+id).append("Loading...");
    if (language == "english") {
        load_text = '[ Loading...... ]';
    } else {
        load_text = '<td colspan="3">[ 下载中，请稍后...... ]</td>';
    }
    $.ajax({
        type: type,
        async: async,
        url: url,
        cache: false,
        dataType: dataType,
        data: data,
        beforeSend: function () {
            $("#" + id).html(load_text)
        },
        error: function (jqXHR, textStatus, errorThrown) {
            //alert(jqXHR.responseText);
            //alert(jqXHR.status);
            //alert(jqXHR.readyState);
            alert(jqXHR.status + " " + jqXHR.statusText);
            //alert(textStatus);
            //alert(errorThrown);
        },
        success: function (msg) {
            //alert(msg);
            //$("#" + id).empty().append(msg);
            $("#" + id).empty().replaceWith(msg);
//            initPage();
//            $("#" + id).removeClass("loader");
        }
    });
}

function replacePage(parameters) {
    var id = parameters.id;
    var type = parameters.type;
    var language = parameters.language;
    var url = parameters.url;
    var async = parameters.async;
    var cache = parameters.cache;
    var data = parameters.data;
    var dataType = parameters.dataType;
    if ("undefined" == typeof type) {
        type = "get";
    }
    if ("undefined" == typeof language) {
        language = "chinese";
    }
    if ("undefined" == typeof async) {
        async = true;
    }
    if ("undefined" == typeof cache) {
        cache = false;
    }
    if ("undefined" == typeof dataType) {
        dataType = 'html';
    }

    //alert(url);
    if (language == "english") {
        load_text = '[ Loading...... ]';
    } else {
        load_text = '[ 下载中，请稍后...... ]';
    }
    $.ajax({
        type: type,
        url: url,
        dataType: dataType,
        data: data,
        beforeSend: function () {
            $("#" + id).html(load_text)
        },
        error: function (jqXHR, textStatus, errorThrown) {
            //alert(jqXHR.responseText);
            //alert(jqXHR.status);
            //alert(jqXHR.readyState);
            alert(jqXHR.status + " " + jqXHR.statusText);
            //alert(textStatus);
            //alert(errorThrown);
        },
        success: function (msg) {
            $("#" + id).html(msg);
        }
    });
}

function deleteData(parameters) {
    var id = parameters.id;
    var type = parameters.type;
    var language = parameters.language;
    var url = parameters.url;
    var async = parameters.async;
    var cache = parameters.cache;
    var data = parameters.data;
    var dataType = parameters.dataType;
    if ("undefined" == typeof type) {
        type = "get";
    }
    if ("undefined" == typeof language) {
        language = "chinese";
    }
    if ("undefined" == typeof async) {
        async = true;
    }
    if ("undefined" == typeof cache) {
        cache = false;
    }
    if ("undefined" == typeof dataType) {
        dataType = 'html';
    }

    //alert(url);
    if (language == "english") {
        load_text = '[ Delete...... ]';
    } else {
        load_text = '<td colspan="3">[ 删除中，请稍后...... ]</td>';
    }
    $.ajax({
        type: type,
        url: url,
        dataType: dataType,
        data: data,
        beforeSend: function () {
            $("#" + id).html(load_text)
        },
        error: function (jqXHR, textStatus, errorThrown) {
            //alert(jqXHR.responseText);
            //alert(jqXHR.status);
            //alert(jqXHR.readyState);
            alert(jqXHR.status + " " + jqXHR.statusText);
            //alert(textStatus);
            //alert(errorThrown);
        },
        success: function (msg) {
            $("#" + id).html(msg);
        }
    });
}

function postData(parameters) {
    var id = parameters.id;
    var type = parameters.type;
    var language = parameters.language;
    var url = parameters.url;
    var async = parameters.async;
    var cache = parameters.cache;
    var data = parameters.data;
    var dataType = parameters.dataType;
    if ("undefined" == typeof type) {
        type = "POST";
    }
    if ("undefined" == typeof language) {
        language = "chinese";
    }
    if ("undefined" == typeof async) {
        async = true;
    }
    if ("undefined" == typeof cache) {
        cache = false;
    }
    if ("undefined" == typeof dataType) {
        dataType = 'json';
    }

    //alert(url);
    if (language == "english") {
        load_text = '[ POST...... ]';
    } else {
        load_text = '<td colspan="3">[ POST中，请稍后...... ]</td>';
    }

    $.ajax({
        //cache: true,
        type: type,
        url: url,
        data: data,
        dataType: dataType,
        //async: async,
        //processData: false,
        //contentType: false,
        beforeSend: function () {
            $("#" + id).html(load_text)
        },
        error: function (jqXHR, textStatus, errorThrown) {
            //alert(jqXHR.responseText);
            //alert(jqXHR.status);
            //alert(jqXHR.readyState);
            alert(jqXHR.status + " " + jqXHR.statusText);
            //alert(textStatus);
            //alert(errorThrown);
        },
        success: function (msg) {
            $("#" + id).html(msg);
        }
    });
}

/*
 var postUrl="http://localhost:7777/fileupload/upload?callback=?";
 var postData = new FormData(document.getElementById("import-form"));
 $.ajax({
 url: postUrl,
 type: "POST",
 dataType: "jsonp",
 data: postData,
 contentType: "application/json",
 processData: false,
 //            contentType: false,
 success: function (data) {
 alert("aa");
 },
 error: function (XMLHttpRequest, textStatus, errorThrown) {
 alert(XMLHttpRequest.status);
 alert(XMLHttpRequest.readyState);
 alert(textStatus);
 }
 });
 $.jsonp({
 "url": postUrl,
 "success": function(data) {
 alert("aa");
 },
 error: function (XMLHttpRequest, textStatus, errorThrown) {
 alert(XMLHttpRequest.status);
 alert(XMLHttpRequest.readyState);
 alert(textStatus);
 }
 });
 */

/*

 function postData(id, postUrl, postData, renderUrl) {
 $.ajax({
 //cache: true,
 type: "POST",
 url: postUrl,
 data: postData,
 //async: true,
 beforeSend: function () {
 $("#" + id).html("Loading......");
 },
 error: function (request) {
 alert("Connection error");
 },
 success: function (msg) {
 //alert('Data saved: ' + msg);
 if (renderUrl == '') {
 $("#" + id).empty().append(msg);
 } else {
 $("#" + id).empty();
 $("#" + id).addClass("loader");
 loadPage({id: id, type: renderUrl});
 $("#" + id).removeClass("loader");
 }
 }
 });
 }
 */

function loadTablePage(id, pageIndex, url) {
    $("#" + id).empty().addClass("loader").append("Loading...");
    $.ajax({
        url: url,
        type: 'GET',
        dataType: 'html',
        data: {"page": pageIndex},
        error: function (msg) {
            alert("Get data error:" + msg);
        },
        success: function (msg) {
            $("#" + id).empty().append(msg);
            initStripeTable();
            $("#" + id).removeClass("loader");
        }
    });
}

function disableForm(formId, isDisabled) {

    var attr = "disable";
    if (!isDisabled) {
        attr = "enable";
    }
    $("form[id='" + formId + "'] :text").attr("disabled", isDisabled);
    $("form[id='" + formId + "'] textarea").attr("disabled", isDisabled);
    $("form[id='" + formId + "'] select").attr("disabled", isDisabled);
    $("form[id='" + formId + "'] :radio").attr("disabled", isDisabled);
    $("form[id='" + formId + "'] :checkbox").attr("disabled", isDisabled);
    $("form[id='" + formId + "'] :button").attr("disabled", isDisabled);

    //禁用jquery easyui中的下拉选（使用input生成的combox）

    $("#" + formId + " input[class='combobox-f combo-f']").each(function () {
        if (this.id) {
            alert("input" + this.id);
            $("#" + this.id).combobox(attr);
        }
    });

    //禁用jquery easyui中的下拉选（使用select生成的combox）
    $("#" + formId + " select[class='combobox-f combo-f']").each(function () {
        if (this.id) {
            alert(this.id);
            $("#" + this.id).combobox(attr);
        }
    });

    //禁用jquery easyui中的日期组件dataBox
    $("#" + formId + " input[class='datebox-f combo-f']").each(function () {
        if (this.id) {
            alert(this.id)
            $("#" + this.id).datebox(attr);
        }
    });
}

/************login begin*******************************/
function Login(postUrl, postData, renderUrl) {
    if (Check()) {
        //LoginSuccess(postUrl, postData, renderUrl);
        //alert('success');
    }
}

function Check() {
    //var reg = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+((\.[a-zA-Z0-9_-]{2,3}){1,2})$/;

    if ($("#username").val() == "") {
        alert("Username cann't be empty.");
        //$("#message").html("Username cann't be empty.");
        $("#username").focus();
        $(":submit").attr("disabled", false);
        return false;
    }
    /*  if(! reg.test($("#"+usernameid).val())){
     //alert("please input correct username format.");
     $("#message").html("please input correct username format.");
     $("#"+usernameid).focus();
     return false;
     }
     */
    if ($("#pass").val() == "") {
        alert("Password can't be empty.");
        //$("#message").html("Password can't be empty.");
        $("#pass").focus();
        $(":submit").attr("disabled", false);
        return false;
    }
    return true;
}

//function LoginSuccess(postUrl, postData, renderUrl)
{
    $.ajax({
        type: "POST",
        url: postUrl,
        data: postData,
        //async: true,
        beforeSend: function () {
            $("#message").html("loading...");
        },
        error: function (request) {
            alert("Connection error");
        },
        success: function (msg) {
            alert('Data saved: ' + msg);
            if ("" == msg) {
                alert('aa1');
                //document.myForm.submit();
            }
            else {
                window.location.href = renderUrl;
                //$("#message").html(decodeURI(msg));
            }
        }
    });
}
/************login end*******************************/

/*
 function changeTableTd1(obj, aTxtModify, aTxtSave, postUrl,precount=1){
 a = obj;
 count = 1;
 var td =a.parent();
 for (count=1;count<=precount;count++)
 {
 td = td.prev();
 }
 var s=td.children('span'),i=td.children('input');
 //alert(a.text());
 if($.trim(a.text())== aTxtModify){
 a.text(aTxtSave).addClass("glyphicon glyphicon-save");
 s.hide();
 i.show()
 }else{
 $.post(postUrl,{id:a.attr('rel'),num:i.val()},function(r){
 //alert(r.msg);
 if(r.status){
 //s.html('Inv.'+r.num+' pieces');
 s.html(r.num);
 //a.text(aTxtModify).removeClass('td-success');
 s.show();
 i.hide()
 }
 },'JSON')
 }
 }

 function changeTableTd2(obj, aTxtModify, aTxtSave, postUrl,precount=1,idprecount=2){
 a = obj;
 count = 1;
 var td =a.parent();
 for (count=1;count<=precount;count++)
 td = td.prev();
 for (count=1;count<=idprecount;count++)
 tdid = td.prev();
 var s=td.children('span'),i=td.children('input');
 //alert(a.text());
 if($.trim(a.text())== aTxtModify){
 a.text(aTxtSave).addClass("glyphicon glyphicon-save");
 s.hide();
 i.show()
 }else{
 //alert(a.text());
 $.ajax({
 type: "POST",
 url: postUrl,
 data: {input:i.val(),id:tdid.val()},
 error: function(request) {
 alert("Connection error");
 },
 success: function(r) {
 alert('Data saved: ' + r);
 var x = eval( "(" + r + ")" );
 s.html(x.input);
 a.text(aTxtModify).removeClass("glyphicon glyphicon-save").addClass("glyphicon glyphicon-edit");
 s.show();
 i.hide();
 }
 },'JSON')
 }
 }
 */

/*
 * e is element;ea means element <a>, einput means element <input>
 * einputtd means element td which include input.
 */
//function changeTableTd(obj, aTxtModify, aTxtSave, postUrl,precount=1,idprecount=2){
function changeTableTd(obj, aTxtModify, aTxtSave, postUrl, precount, idprecount) {
    ea = obj;
    count = 1;
    var eatd = ea.parent();
    var eedittd = eatd;
    var eidtd = eatd;
    for (count = 1; count <= precount; count++)
        eedittd = eedittd.prev();
    for (count = 1; count <= idprecount; count++)
        eidtd = eidtd.prev();
    var eeditspan = eedittd.children('span'), eeditinput = eedittd.children('input');
    var eidspan = eidtd.children('input');
    //alert(ea.text());
    if ($.trim(ea.text()) == aTxtModify) {
        ea.text(aTxtSave).addClass("glyphicon glyphicon-save");
        eeditspan.hide();
        eeditinput.show();
    } else {
        $.ajax({
            type: "POST",
            url: postUrl,
            data: {input: eeditinput.val(), id: eidspan.val()},
            error: function (request) {
                alert("Connection error");
            },
            success: function (r) {
                //alert('Data saved: ' + r);
                //alert(r);
                var x = eval("(" + r + ")"); //var re = $.parseJSON(data);?
                eeditspan.html(x.input);
                ea.text(aTxtModify).removeClass("glyphicon glyphicon-save").addClass("glyphicon glyphicon-edit");
                eeditspan.show();
                eeditinput.hide();
            }
        }, 'JSON')
    }
}

function changeTableTwoTd(obj, aTxtModify, aTxtSave, postUrl) {
    a = obj;
    var td = a.parent().prev(), s = td.children('span'), i = td.children('input');
    var td2 = td.prev(), s2 = td2.children('span'), i2 = td2.children('input');
    var td3 = td2.prev(), i3 = td3.children('input');
    //alert(a.text());
    if ($.trim(a.text()) == aTxtModify) {
        a.text(aTxtSave).addClass("fa fa-save fa-1g");
        s.hide();
        i.show();
        s2.hide();
        i2.show();
    }
    else {
        $.ajax({
            type: "POST",
            url: postUrl,
            data: {input: i.val(), input2: i2.val(), input3: i3.val()},
            error: function (request) {
                alert("Connection error");
            },
            success: function (r) {
                //alert('Data saved: ' + r);
                var x = eval("(" + r + ")");
                s.html(x.input);
                a.text(aTxtModify).removeClass("fa fa-save fa-1g").addClass("fa fa-edit fa-1g");
                s.show();
                i.hide();
                s2.html(x.input2);
                s2.show();
                i2.hide();
            }
        });
        /*
         $.post(postUrl,{id:a.attr('rel'),updateid:td.children('span').val(),input:i.val(),input2:i2.val()},function(r){
         alert(r.msg);
         if(r.status){
         alert('Data saved: ' + r);
         s.html(r.input);
         //a.text(aTxtModify).removeClass('td-success');
         s.show();
         i.hide();
         s2.html(r.input2);
         s2.show();
         i2.hide();
         }
         },'JSON')*/
    }
}

/***************************************************
 responsive menu
 ***************************************************/
function responsivemenu() {
    //alert('279');
    $("#cat-navi").append("<select/>");
    $("#cat-navi select").addClass("form-control");
    $("<option />", {
        "selected": "selected",
        "value": "",
        "text": "Choose category"
    }).appendTo("#cat-navi select");
    //new dropdown menu
    $("#cat-navi a").each(function () {
        var el = $(this);
        var perfix = '';
        switch (el.parents().length) {
            case (11):
                perfix = '-';
                break;
            case (13):
                perfix = '--';
                break;
            default:
                perfix = '';
                break;

        }

        $("<option />", {
            "value": el.attr("href"),
            "text": perfix + el.text()
        }).appendTo("#cat-navi select");
    });

    $('#cat-navi select').change(function () {

        window.location.href = this.value;

    });
}
/****************************************************/

function switch_sidebar_item(obj) {
    $('#cat-navi .list-group-item').removeClass("active");
    obj.addClass("active");
}

